package com.jwb.pub.controller;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.util.ToolsUtil;
@Slf4j
@Controller
@RequestMapping(value="/pub/common")
public class CommonController {
	@Autowired
	private BaseDAO baseDAO;

	  @RequestMapping("/getConfig")
	  @ResponseBody
	  public String getConfig(HttpServletRequest req) throws Exception{
		  JSONObject jsobject  = new JSONObject();
		  String prop = req.getParameter("prop");
		  String config = req.getParameter("config")==null?"config.properties":req.getParameter("config");
		  if(prop==null||prop.equals("")){
			  jsobject.put("respCode", "9");
			  jsobject.put("respDesc", "");
			  return jsobject.toString();
		  }
		  try{
			  String result = ToolsUtil.getProp(config, prop);
			  jsobject.put("respCode", "0");
			  jsobject.put("respDesc", "OK");
			  jsobject.put("value", result);
		  }catch(Exception e){
			  jsobject.put("respCode", "-1");
			  jsobject.put("respDesc", "获取 "+prop+" 属性失败");
		  }
		  return jsobject.toString();
	  }
	  
	  @RequestMapping("/getVersion")
	  @ResponseBody
	  public String getVersion(HttpServletRequest req) throws Exception{
			JSONObject jsobject = new JSONObject();;
			String typeName=req.getParameter("typeName");
			jsobject = baseDAO.findBySqlToJosn("select type_name, code, code_desc, remarks from sundry where type_name='"+typeName+"'");
			log.info("getVersion():" + jsobject.toString());
			return jsobject.toString();
	  }
	  /**
	   *  http://127.0.0.1:8080/kpshow/common/getDict.mvc
	   * @param req
	   * @return
	   * @throws Exception
	   */
	  @RequestMapping("/getDict")
	  @ResponseBody
	  public String getDict(HttpServletRequest req) throws Exception{
			JSONObject jsobject = new JSONObject();	
			JSONObject jso = new JSONObject();	
			String sql="";
			jso = baseDAO.findBySqlToJosn("select pub.get_dict_sql str from dual");
			sql=jso.getJSONArray("list").getJSONObject(0).getString("str");			
			log.info("getDict():" + sql);
			jsobject = baseDAO.findBySqlToJosn(sql);
			log.info("getDict():" + jsobject.toString());
			
			return jsobject.toString();
	  }
	
}
